package com.example;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ServicesDemo extends Activity implements OnClickListener {
  private static final String TAG = "ServicesDemo";
  Button buttonStart, buttonStop;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    File fl = new File("myfile");
    FileInputStream fin;
    String t[]=null;
	try {
		fin = new FileInputStream(fl);String ret = convertStreamToString(fin);   fin.close();
		t=ret.split("|");
	} 
	catch (FileNotFoundException e) { 
		String b="a-true b-true";
		t=b.split(" ");
		}
	catch (Exception e) {}
    buttonStart = (Button) findViewById(R.id.buttonStart);
    buttonStop = (Button) findViewById(R.id.buttonStop);
    buttonStart.setEnabled(dd(t[0]));
    buttonStop.setEnabled(dd(t[1]));
    buttonStart.setOnClickListener(this);
    buttonStop.setOnClickListener(this);
  }
  boolean dd(String h){
	  String[] t=h.split("-");
	  boolean v=(t[1].equals("true"))?true:false;
	  return v;
  }
  String convertStreamToString(InputStream is) throws Exception {
	    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
	    StringBuilder sb = new StringBuilder();
	    String line = null;
	    while ((line = reader.readLine()) != null) {
	      sb.append(line).append("\n");
	    }
	    return sb.toString();
	}

  public void onClick(View src) {
    switch (src.getId()) {
    case R.id.buttonStart:
      Log.d(TAG, "onClick: starting srvice");
  
      startService(new Intent(this, MyService.class));
      String filename = "myfile";
      String string = "start-false stop-true";
      FileOutputStream outputStream;

      try {
        outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
        outputStream.write(string.getBytes());
        outputStream.close();
        buttonStart.setEnabled(false);
        buttonStop.setEnabled(true);
      } catch (Exception e) {
        e.printStackTrace();
      }
      break;
    case R.id.buttonStop:
      Log.d(TAG, "onClick: stopping srvice");
      stopService(new Intent(this, MyService.class));
      String filename1 = "myfile";
      String string1 = "start-true stop-false";
      FileOutputStream outputStream1;

      try {
        outputStream1 = openFileOutput(filename1, Context.MODE_PRIVATE);
        outputStream1.write(string1.getBytes());
        outputStream1.close();
        buttonStart.setEnabled(true);
        buttonStop.setEnabled(false);
      } catch (Exception e) {
        e.printStackTrace();
      }
      break;
    }
  }
}